// Include standard headers
#include <stdio.h>
#include <stdlib.h>

// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <GL/glfw.h>

#include <glm/glm.hpp>
using namespace glm;

#include "common/shader.hpp"

int main( void )
{
    
    if (!glfwInit())
    {
        /* code */
        fprintf(stderr, "%s\n", "fail to init GLFW");
    }
    
    glfwOpenWindowHint(GLFW_FSAA_SAMPLES,4);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR,2);
    
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR,1);

    if (!glfwOpenWindow(1024,678,0,0,0,0,32,0,GLFW_WINDOW))
    {
        fprintf(stderr, "%s\n", "fail to open glfw window");
        /* code */
        glfwTerminate();
        return -1;
    }
    //init glew
    if (glewInit() != GLEW_OK)
    {
        fprintf(stderr, "%s\n", "failed to init glew");
        return -1;
        /* code */
    }
    glfwSetWindowTitle("02");
    glfwEnable(GLFW_STICKY_KEYS);
    glClearColor(0.0f,0.0f,0.0f,0.0f);

    GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader"  );
    GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
    static const GLfloat g_vertex_buffer_data[] = { 

        -1.0f, -1.0f, 0.0f,

         1.0f, -1.0f, 0.0f,

         0.0f,  1.0f, 0.0f,

    };



    GLuint vertexbuffer;

    glGenBuffers(1, &vertexbuffer);

    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);



    do{



        // Clear the screen

        glClear( GL_COLOR_BUFFER_BIT );



        // Use our shader

        //glUseProgram(programID);



        // 1rst attribute buffer : vertices

        //glEnableVertexAttribArray(vertexPosition_modelspaceID);
        glEnableVertexAttribArray(0);

        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

        glVertexAttribPointer(

           // vertexPosition_modelspaceID, // The attribute we want to configure

                0,
            3,                  // size

            GL_FLOAT,           // type

            GL_FALSE,           // normalized?

            0,                  // stride

            (void*)0            // array buffer offset

        );



        // Draw the triangle !

        glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle



        //glDisableVertexAttribArray(vertexPosition_modelspaceID);
        glDisableVertexAttribArray(0);



        // Swap buffers

        glfwSwapBuffers();



    } // Check if the ESC key was pressed or the window was closed

    while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&

           glfwGetWindowParam( GLFW_OPENED ) );



    // Close OpenGL window and terminate GLFW

    glfwTerminate();



    // Cleanup VBO

    glDeleteBuffers(1, &vertexbuffer);
	return 0;
}

